#!/bin/bash

set -e

<%
  tsa_addrs = nil
  tsa_port = nil

  if_p("tsa.host", "tsa.port") do |host, port|
    tsa_addrs = [host]
    tsa_port = port
  end

  if tsa_addrs.nil? && tsa_port.nil?
    tsa = link("tsa")
    tsa_addrs = tsa.instances.collect(&:address)
    tsa_port = tsa.p("bind_port")
  end
%>

RUN_DIR=/var/vcap/sys/run/groundcrew
LOG_DIR=/var/vcap/sys/log/groundcrew
PIDFILE=$RUN_DIR/beacon.pid

source /var/vcap/packages/pid_utils/pid_utils.sh

kill_session_siblings_of() {
  pidfile=$1

  if [ -f "$pidfile" ]; then
    pid=$(head -1 "$pidfile")

    session=$(ps -p $pid -o sid=)
    ps -s $session -o pid:1= | xargs kill

    rm -f $pidfile
  else
    echo "Pidfile $pidfile doesn't exist!"
  fi
}

case $1 in

  start)
    pid_guard $PIDFILE "beacon"

    mkdir -p $RUN_DIR
    chown -R vcap:vcap $RUN_DIR

    mkdir -p $LOG_DIR
    chown -R vcap:vcap $LOG_DIR

    echo $$ > $PIDFILE

    for key in /var/vcap/packages/generated_worker_key/id_rsa \
               /var/vcap/jobs/groundcrew/config/private_key; do
      chown vcap:vcap $key
      chmod 0400 $key
    done

    # even more ideal would be for each node to have its own key and stop doing
    # the generated_tsa_host_key thing
    {
      <% tsa_addrs.each do |addr| %>
      echo -n '[<%= addr %>]:<%= tsa_port %> '
      cat /var/vcap/packages/generated_tsa_host_key/id_rsa.pub
      <% end %>
    } > /var/vcap/jobs/groundcrew/config/generated_tsa_known_hosts

    KNOWN_HOSTS_FILE=${RUN_DIR}/known_hosts

    cat \
      /var/vcap/jobs/groundcrew/config/static_known_hosts \
      /var/vcap/jobs/groundcrew/config/generated_tsa_known_hosts \
      > $KNOWN_HOSTS_FILE

    chown vcap:vcap $KNOWN_HOSTS_FILE

    cat /var/vcap/jobs/groundcrew/config/worker.json | \
      sed -e "s/HOSTNAME_REPLACE_ME/$(hostname | cut -d '-' -f 2-)/g" | \
      sed -e 's/"START_TIME_REPLACE_ME"/'$(date +%s)'/g' | \
      /var/vcap/packages/resource_discovery/bin/resource_discovery /var/vcap/packages/*/resource_metadata.json > \
      $RUN_DIR/worker.json

    tsa_addrs=(<%= tsa_addrs.join(" ") %>)

    while true; do
      rand=$RANDOM
      tsa_addr=${tsa_addrs[$rand % ${#tsa_addrs[@]}]}

      chpst -u vcap:vcap ssh \
        -p <%= tsa_port %> $tsa_addr \
        <% if p("tsa.private_key") != "" %> \
        -i /var/vcap/jobs/groundcrew/config/private_key \
        <% else %> \
        -i /var/vcap/packages/generated_worker_key/id_rsa \
        <% end %> \
        -o UserKnownHostsFile=${KNOWN_HOSTS_FILE} \
        -o ConnectTimeout=30 \
        -o ServerAliveInterval=8 \
        -o ServerAliveCountMax=3 \
        <% if p("garden.forward_address", nil) %> \
          -R0.0.0.0:7777:<%= p("garden.forward_address") %> \
          <% if p("baggageclaim.forward_address", nil) %> \
          -R0.0.0.0:7788:<%= p("baggageclaim.forward_address") %> \
          <% end %> \
          forward-worker \
          --garden 0.0.0.0:7777 \
          <% if p("baggageclaim.forward_address", nil) %> \
          --baggageclaim 0.0.0.0:7788 \
          <% end %> \
        <% else %> \
          register-worker \
        <% end %> \
        < $RUN_DIR/worker.json \
        1>>$LOG_DIR/beacon.stdout.log \
        2>>$LOG_DIR/beacon.stderr.log || \
        true

      sleep 1
    done

    ;;

  stop)
    kill_session_siblings_of $PIDFILE

    ;;

  *)
    echo "Usage: $0 {start|stop}"

    ;;

esac
